<template>
  <rx-dialog @handOk="addAdmin" :oktext="i18n('list.addAdmin','添加管理员')" btnalign="left" order="top" @cancel="cancel">
    <div>
      <a-table
        rowKey="userId"
        :pagination="false"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        :columns="columns"
        :dataSource="users">
          <span slot="action" slot-scope="text, record">
            <a  v-if="record.admin==1" :disabled="true">{{i18n('list.del','移除')}}</a>
            <a  v-if="record.admin==2" @click="removeAdmin(record)">{{i18n('list.del','移除')}}</a>
          </span>
      </a-table>
    </div>
  </rx-dialog>
</template>

<script>
    import {Dialog, RxDialog, Util} from "@lowcode/jpaas-common-lib"
    import OsUserApi from '@/api/user/org/osUser'

    export default {
        name: "OsInstAdmin",
        components: {
            RxDialog
        },
        props: ["layerid","destroy","tenantId"],
        data() {
            return {
                selectedRowKeys: [],
                selectedRows: [],
                users: [],
                columns: [{
                    title: this.$ti18('rxList.action','操作'),
                    indentWidth: '180px',
                    dataIndex: 'action',
                    scopedSlots: {customRender: 'action'}
                }, {
                    title: this.i18n('form.fullName','姓名'),
                    dataIndex: 'fullName'
                }, {
                    title: this.i18n('form.userNo','编号'),
                    dataIndex: 'userNo'
                }]
            }
        },

        created() {
            this.init();
        },
        methods: {
            i18n(name,text,key){
                return this.$ti18(name,text,"osInst",key);
            },
            init(){
                OsUserApi.getAdmin(this.tenantId).then(res=>{
                    this.users=res;
                })
            },
            addAdmin(e) {
                var params={single:false};
                if(this.tenantId){
                    params.tenantId=this.tenantId;
                }

                var self_=this;
                e.loading=false;
                Dialog.openUserDialog({
                    curVm: this,data:params, widthHeight: ['900px', '600px']
                },function(self,users) {
                    var ary=users.map(item=>{
                        return item.userId;
                    });
                    var userIds=ary.join(",");
                    OsUserApi.setAdmin(userIds,2,self_.tenantId).then(res=>{
                        self_.init();
                        e.loading=false;
                    });
                })
            },
            cancel() {
                Util.closeWindow(this,"cancel")
            },
            removeAdmin(record){
                var self_=this;

                this.$confirm({
                    title: this.i18n('tipsDialog.title','提示信息'),
                    content: this.i18n('tipsDialog.content','确认清除管理员吗？'),
                    okText: this.$ti18('rxForm.okBtn','确定'),
                    cancelText: this.$ti18('rxForm.cancelBtn','取消'),
                    zIndex:20000,
                    onOk() {
                        OsUserApi.setAdmin(record.userId,0,self_.tenantId).then(res=>{
                            self_.init();
                        });
                    }
                });
            },
            onSelectChange (selectedRowKeys, selectedRows) {
                this.selectedRowKeys = selectedRowKeys
                this.selectedRows = selectedRows
            }
        }
    }
</script>

<style scoped>

</style>